FROM php:8.2.16-cli-alpine3.18
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
	APP_ENV=prod \
    SCAN_CACHEABLE=(true) \
    APP_SYSTEM_ENV=docker \
	COMPOSER_CDN=https://packagist.org \
	COMPOSER_ALLOW_SUPERUSER=1 \
	#chromium
	CHROME_PATH=/usr/bin/chromium-browser \
    # Chromium and ChromeDriver
    PANTHER_NO_SANDBOX=1 \
    # Not mandatory, but recommended
    PANTHER_CHROME_ARGUMENTS='--disable-dev-shm-usage'

# PHPIZE_DEPS 中已包含 autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c

COPY ./fonts/AppleColorEmoji.ttc /usr/share/fonts/custom/AppleColorEmoji.ttc
COPY ./fonts/PingFang.ttc /usr/share/fonts/custom/PingFang.ttc

RUN \
    curl -sfL https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer --version=2.5.8 \
    && chmod +x /usr/bin/composer \
    && composer config -g repo.packagist composer ${COMPOSER_CDN} \
    && apk update \
    # 可选 python3 库 \
    && apk add --no-cache python3 python3-dev py3-pip \
    # tesseract-ocr
    && apk add --no-cache git expect openssh-client openssl bash ffmpeg exiftool libzip  \
    #chromium browser and pdf generate \
    # ttf-freefont font-noto-cjk ttf-dejavu ttf-droid 这些字体省略
    && apk add --no-cache chromium chromium-chromedriver harfbuzz nss freetype dbus mesa-gbm mesa-gl mesa-egl xvfb alsa-lib libxcomposite libxdamage libxfixes libxi libxtst udev nodejs npm fontconfig qpdf \
    #other
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS cmake libstdc++ openssl-dev zlib zlib-dev gmp-dev libzip-dev curl-dev pcre-dev pcre2-dev libtool linux-headers build-base freetype-dev libjpeg-turbo-dev libpng-dev yasm \
	# download
	&& git clone https://gitee.com/lujihong/docker.git /tmp/docker \
	&& ls -alh && sleep 1 \
	# create /usr/src/php/ext
	&& docker-php-source extract \
    # move files
    && mv /tmp/docker/xlswriter-1.5.5 /usr/src/php/ext/xlswriter \
    && ls -l /tmp/docker/sgloader/ \
    && mv /tmp/docker/sgloader/ixed.8.2.lin /usr/local/lib/php/extensions/no-debug-non-zts-20220829/ixed.8.2.lin \
    && mv /tmp/docker/swow-1.4.1/ext /usr/src/php/ext/swow \
    && mv /tmp/docker/phpy-1.0.4 /usr/src/php/ext/phpy \
    #chromium fonts
    #&& mv /tmp/docker/fonts /usr/share/fonts/custom \
    && fc-cache -f -v \
	# install ext
	&& pecl update-channels \
	&& pecl install redis \
    && docker-php-ext-enable redis \
    && docker-php-ext-install -j$(nproc) gmp \
    && docker-php-ext-configure zip --with-zip --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install zip \
    && docker-php-ext-install sockets \
	&& docker-php-ext-install bcmath \
	&& docker-php-ext-install ctype \
	&& docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg \
	&& docker-php-ext-install -j$(nproc) gd \
	&& docker-php-ext-install pdo_mysql \
	&& docker-php-ext-install pcntl \
	&& docker-php-ext-install opcache \
	&& docker-php-ext-configure sysvshm \
		--with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvshm \
	&& docker-php-ext-configure sysvmsg \
	    --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvmsg \
	&& docker-php-ext-configure sysvsem \
	    --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvsem \
	# xlswriter
	&& docker-php-ext-configure xlswriter \
	    --enable-reader \
	&& docker-php-ext-install -j$(nproc) xlswriter \
    # swow
    && docker-php-ext-configure swow \
      --enable-swow  \
      --enable-swow-ssl  \
      --enable-swow-curl \
    && docker-php-ext-install -j$(nproc) swow \
    # phpy
    && docker-php-ext-configure phpy \
          --with-python-config=/usr/bin/python3-config \
    && docker-php-ext-install -j$(nproc) phpy \
    && docker-php-ext-enable phpy \
	#  ---------- some config ----------
	&& echo "opcache.enable_cli = 'On'" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini \
    && echo "extension=ixed.8.2.lin" > /usr/local/etc/php/conf.d/docker-php-ext-sgloader.ini \
	# config PHP
	&& cd /usr/local/etc/php \
	&& { \
	    echo "upload_max_filesize=128M"; \
	    echo "post_max_size=128M"; \
	    echo "memory_limit=1G"; \
	    echo "date.timezone=${TIMEZONE}"; \
	} | tee conf.d/99_overrides.ini \
	# config timezone
	&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
	&& echo "${TIMEZONE}" > /etc/timezone \
	# php info
	&& php -v \
	&& php -m \
    && php --ri swow \
	&& composer \
	# clear
	&& docker-php-source delete \
	&& apk del .build-deps \
	&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man /usr/src/php.tar.xz* $HOME/.composer/*-old.phar \
	&& echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR "/opt/www/"

# Composer Cache
# COPY ./composer.* /opt/www/
# RUN composer install --no-dev --no-scripts

#EXPOSE 9501 9502

ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]